package org.wlw.user;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class UserManager {
	private Map<String,User> users;
	
	public UserManager() {
		users = new HashMap<String,User>();
	}
	
	public User load(String username) {
		return users.get(username);
	}
	
	public void list() {
		System.out.println("\t用户名\t\t用户昵称\t\t年龄\t\t密码");
		System.out.println("**********************************************************************************");
		Set<String> keys = users.keySet();
		for(String key:keys) {
			System.out.println(users.get(key));
		}
	}
	
	public void add(User user) {
		if(users.containsKey(user.getUsername())) {
			System.out.println("用户已经存在，不能添加");
			return;
		}
		users.put(user.getUsername(),user);
	}
	
	
	public void delete(String username) {
		User tu = load(username);
		if(tu==null) {
			System.out.println("删除的用户不存在!");
			return;
		}
		users.remove(username);
	}
	
	public void update(User user) {
		User tu = this.load(user.getUsername());
		if(tu==null) {
			System.out.println("更新的用户不存在!");
			return;
		}
		tu.setAge(user.getAge());
		tu.setNickname(user.getNickname());
		tu.setPassword(user.getPassword());
	}
	
	public User login(String username,String password) {
		User tu = this.load(username);
		if(tu==null) {
			System.out.println("用户不存在！");
			return null;
		}
		if(!tu.getPassword().equals(password)) {
			System.out.println("密码不正确!");
			return null;
		}
		return tu;
	}
}
